package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizCouponGoods;
import com.member.shop.domain.SysBizCouponGoodsUser;
import com.member.shop.mapper.SysBizCouponGoodsUserMapper;
import com.member.shop.service.ISysBizCouponGoodsUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 用户优惠券指定商品Service业务层处理
 *
 * @author lixinchen
 * @date 2024-05-11
 */
@Service
public class SysBizCouponGoodsUserServiceImpl extends ServiceImpl<SysBizCouponGoodsUserMapper, SysBizCouponGoodsUser> implements ISysBizCouponGoodsUserService {
    @Resource
    private SysBizCouponGoodsUserMapper sysBizCouponGoodsUserMapper;

    /**
     * 查询用户优惠券指定商品
     *
     * @param id 用户优惠券指定商品主键
     * @return 用户优惠券指定商品
     */
    @Override
    public SysBizCouponGoodsUser selectSysBizCouponGoodsUserById(Long id) {
        return sysBizCouponGoodsUserMapper.selectById(id);
    }

    /**
     * 查询用户优惠券指定商品列表
     *
     * @param queryWrapper 用户优惠券指定商品
     * @return 用户优惠券指定商品
     */
    @Override
    public List<SysBizCouponGoodsUser> selectSysBizCouponGoodsUserList(LambdaQueryWrapper<SysBizCouponGoodsUser> queryWrapper) {
        return sysBizCouponGoodsUserMapper.selectList(queryWrapper);
    }

    /**
     * 新增用户优惠券指定商品
     *
     * @param sysBizCouponGoodsUser 用户优惠券指定商品
     * @return 结果
     */
    @Override
    public int insertSysBizCouponGoodsUser(SysBizCouponGoodsUser sysBizCouponGoodsUser) {
        sysBizCouponGoodsUser.setCreateTime(DateUtils.getNowDate());
        return sysBizCouponGoodsUserMapper.insert(sysBizCouponGoodsUser);
    }

    /**
     * 修改用户优惠券指定商品
     *
     * @param sysBizCouponGoodsUser 用户优惠券指定商品
     * @return 结果
     */
    @Override
    public int updateSysBizCouponGoodsUser(SysBizCouponGoodsUser sysBizCouponGoodsUser) {
        sysBizCouponGoodsUser.setUpdateTime(DateUtils.getNowDate());
        return sysBizCouponGoodsUserMapper.updateById(sysBizCouponGoodsUser);
    }

    /**
     * 批量删除用户优惠券指定商品
     *
     * @param ids 需要删除的用户优惠券指定商品主键
     * @return 结果
     */
    @Override
    public int deleteSysBizCouponGoodsUserByIds(Long[] ids) {
        return sysBizCouponGoodsUserMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除用户优惠券指定商品信息
     *
     * @param id 用户优惠券指定商品主键
     * @return 结果
     */
    @Override
    public int deleteSysBizCouponGoodsUserById(Long id) {
        return sysBizCouponGoodsUserMapper.deleteById(id);
    }

    @Override
    public SysBizCouponGoodsUser getByUserIdAndReceiveId(Long userId, Long receiveId, Long goodsId) {
        LambdaQueryWrapper<SysBizCouponGoodsUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysBizCouponGoodsUser::getGoodsId, goodsId);
        queryWrapper.eq(SysBizCouponGoodsUser::getReceiveId, receiveId);
        queryWrapper.eq(SysBizCouponGoodsUser::getUserId, userId);
        List<SysBizCouponGoodsUser> list = this.list(queryWrapper);

        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);

    }

    @Override
    public SysBizCouponGoodsUser getByUserIdAndReceiveIdByGoodsIds(Long userId, Long receiveId, List<Long> goodsIdList) {
        LambdaQueryWrapper<SysBizCouponGoodsUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SysBizCouponGoodsUser::getGoodsId, goodsIdList);
        queryWrapper.eq(SysBizCouponGoodsUser::getReceiveId, receiveId);
        queryWrapper.eq(SysBizCouponGoodsUser::getUserId, userId);
        List<SysBizCouponGoodsUser> list = this.list(queryWrapper);

        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<SysBizCouponGoodsUser> getByUserIdAndReceiveIdByGoodsIds2(Long userId, Long receiveId, List<Long> goodsIdList) {
        LambdaQueryWrapper<SysBizCouponGoodsUser> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(SysBizCouponGoodsUser::getGoodsId, goodsIdList);
//        queryWrapper.eq(SysBizCouponGoodsUser::getReceiveId, receiveId);
        queryWrapper.eq(SysBizCouponGoodsUser::getUserId, userId);
        List<SysBizCouponGoodsUser> list = this.list(queryWrapper);

        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list;
    }
}
